//
//  STNetworkTools.m
//  OCVVeibo
//
//  Created by leergou on 16/7/22.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

#import "STNetworkTools.h"
#import "STUserAccountDTO.h"

@implementation STNetworkTools

#pragma mark - 单例
+ (STNetworkTools *)sharedTools{
    
    static STNetworkTools *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[STNetworkTools alloc] init];
        // 第一个是请求账户的返回格式,第二个是请求用户信息的返回格式
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"application/json", nil];
    });
    return instance;
}

#pragma mark - 请求
- (void)request:(RequestType)type urlString:(NSString *)urlString parameters:parameters success:(void(^)(id response))success failure:(void(^)(NSError *error))failure{
    
    if (type == GET) {
        
        [self GET:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            success(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            failure(error);
        }];

        
    } else {
        
        [self POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            success(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            failure(error);
        }];
    }
}

#pragma mark - 获取账号
- (void)oahtLoadUserAccountWithCode:(NSString *)code success:(void(^)(id response))success failure:(void(^)(NSError *error))failure{
    
    NSString *urlString = @"https://api.weibo.com/oauth2/access_token";
    
    NSDictionary *parameters = @{
                                 @"client_id":APPKEY,
                                 @"client_secret":APPSECRET,
                                 @"grant_type":@"authorization_code",
                                 @"code":code,
                                 @"redirect_uri":APPREDIRECT_URI
                                 };
    [self request:(POST) urlString:urlString parameters:parameters success:success failure:failure];
    
}

#pragma mark - 获取用户信息
- (void)oahtLoadUserInfoWithUserAccount:(STUserAccountDTO *)userAccountDTO success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    
    /*
     // 获取用户信息
     let urlString =
     
     let params = [
     "access_token":userAccount.access_token!,
     "uid":userAccount.uid!
     ]
     
     // 发送请求
     request(RequestType.GET, urlString: urlString, parameters: params, success: success, failure: failure)
     */
    
    NSString *urlString = @"https://api.weibo.com/2/users/show.json";
    
    NSDictionary *parameters = @{
                                 @"access_token":userAccountDTO.access_token,
                                 @"uid":userAccountDTO.uid
                                 };
    
    [[STNetworkTools sharedTools] request:(GET) urlString:urlString parameters:parameters success:success failure:failure];
}

#pragma mark - 请求首页数据信息
- (void)homeLoadDataWithSuccess:(void(^)(id response))success failuer:(void(^)(NSError *error))failure{
    
    /*
     https://api.weibo.com/2/statuses/home_timeline.json
     必选	类型及范围	说明
     access_token	true	string	采用OAuth授权方式为必填参数，OAuth授权后获得。
     since_id	false	int64	若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。
     max_id	false	int64	若指定此参数，则返回ID小于或等于max_id的微博，默认为0。
     count	false	int	单页返回的记录条数，最大不超过100，默认为20。
     page	false	int	返回结果的页码，默认为1。
     base_app	false	int	是否只获取当前应用的数据。0为否（所有数据），1为是（仅当前应用），默认为0。
     feature	false	int	过滤类型ID，0：全部、1：原创、2：图片、3：视频、4：音乐，默认为0。
     trim_user	false	int	返回值中user字段开关，0：返回完整user字段、1：user字段仅返回user_id，默认为0。
     */
    
    NSString *urlString = @"https://api.weibo.com/2/statuses/home_timeline.json";
    NSDictionary *parameters = @{
                                 @"access_token":[STUserAccountVM sharedUserAccountTools].accessToken,
                                 };
    
    [[STNetworkTools sharedTools] request:GET urlString:urlString parameters:parameters success:success failure:failure];
    
}


@end














